import {Component, ViewChild} from '@angular/core';
import { Content, IonicPage, NavController, NavParams } from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";

/**
 * Generated class for the FinanceCalendarPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-finance-calendar',
  templateUrl: 'finance-calendar.html',
})
export class FinanceCalendarPage {

    @ViewChild(Content) content: Content;
    tab_position: number = 0;
    date_list: any[] = [];
    week: any = [
        '周日',
        '周一',
        '周二',
        '周三',
        '周四',
        '周五',
        '周六',
    ];

    sub_position: number = 0;
    calendar_list: any[] = [
        '日历指标',
        '财经事件'
    ];

    finance_list: any[] = [];
    event_list: any[] = [];

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private httpService: HttpService) {
        let date = new Date();
        for (let i = 0; i < 7; i++) {
            let showDate = new Date();
            showDate.setDate(date.getDate() + i);
            let title = (showDate.getMonth() + 1) + '-' + showDate.getDate() + ' ' + this.week[showDate.getDay()];
            let value = showDate.getFullYear() + '-' + (showDate.getMonth() + 1) + '-' + showDate.getDate() + ' 00:00:00';
            this.date_list.push({
                title: title,
                value: value
            });
        }
    }

    ionViewDidLoad() {
        this.getFinanceCalendar(this.date_list[this.tab_position].value).then(res => {
            this.finance_list = res.result;
        });
    }

    getItemStarStyle(isShow: boolean) {
        return isShow ? 'ios-star' : 'ios-star-outline';
    }

    getItemPriceStyle(type: string) {
        return {
            'none-tag': type == '0',
            'up-tag': type == '1',
            'down-tag': type == '2'
        };
    }

    getTabBtnStyle(index: number) {
        return {
            'tab-btn-select': this.tab_position == index,
            'tab-btn-none': this.tab_position != index
        };
    }

    getSubBtnStyle(index: number) {
        return {
            'sub-btn-select': this.sub_position == index,
            'sub-btn-none': this.sub_position != index
        };
    }

    getFinanceCalendar(date: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.httpService.instance()
                .setUrl('/gold/getFinanceCalendar')
                .setQuery({
                    date: date
                })
                .showLoader()
                .getRequest()
                .then(res => {
                    if (res.status == 0) {
                        resolve(res.data);
                    } else {
                        reject(res.msg);
                    }
                });
        });
    }

    getFinanceEvent(date: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.httpService.instance()
                .setUrl('/gold/getFinanceEvent')
                .setQuery({
                    date: date
                })
                .showLoader()
                .getRequest()
                .then(res => {
                    if (res.status == 0) {
                        resolve(res.data);
                    } else {
                        reject(res.msg);
                    }
                });
        });
    }

    onTabBtnSelected(index: number, value: string) {
        this.tab_position = index;
        this.sub_position = 0;

        this.content.scrollToTop().then(() => {
            this.getFinanceCalendar(value).then(res => {
                this.finance_list = res.result;
            });
        });
    }

    onSubBtnSelected(index: number) {
        this.sub_position = index;
        switch (index) {
            case 0:
                this.getFinanceCalendar(this.date_list[this.tab_position].value).then(res => {
                    this.finance_list = res.result;
                });
                break;

            case 1:
                this.getFinanceEvent(this.date_list[this.tab_position].value).then(res => {
                    this.event_list = res.result;
                });
                break;
        }
    }

    onScrollTop() {
        this.content.scrollToTop();
    }
}
